#include "Direct3DFramework.h"

Direct3DFramework::Direct3DFramework()
	//window app att
	: m_window(0),

	//direct3d att
	m_object(0),
	m_device(0),
	m_sprite(0),
	m_font(0),
	m_line(0)
{

}

Direct3DFramework::~Direct3DFramework()
{
	//implicitly release
	release();
}

int Direct3DFramework::init(WindowFramework* a_window)
{
	//obtain handle to window object
	m_window = a_window;

	//zero out graphics information structs
	ZeroMemory(&m_present,sizeof(m_present));
	ZeroMemory(&m_display,sizeof(m_display));
	ZeroMemory(&m_capability,sizeof(m_capability));

	//obtain the direct3d object interface
	m_object = Direct3DCreate9(D3D_SDK_VERSION);

	//obtain display mode info
	if(FAILED(m_object->GetAdapterDisplayMode(
		D3DADAPTER_DEFAULT,
		&m_display)))
		return GPX_INIT_DISPLAY_ERROR;

	//obtain device capability info
	if(FAILED(m_object->GetDeviceCaps(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		&m_capability)))
		return GPX_INIT_CAPABILITY_ERROR;

	//set the present parameter struct
	m_present.hDeviceWindow					= m_window->m_hWnd;
	m_present.Windowed						= m_window->m_windowed;
	m_present.SwapEffect					= D3DSWAPEFFECT_DISCARD;
	m_present.BackBufferCount				= 1;
	m_present.BackBufferFormat				= D3DFMT_X8R8G8B8;
	m_present.BackBufferWidth				= m_window->m_windowWidth;
	m_present.BackBufferHeight				= m_window->m_windowHeight;
	m_present.EnableAutoDepthStencil		= true;
	m_present.AutoDepthStencilFormat		= D3DFMT_D16;
	m_present.MultiSampleType				= D3DMULTISAMPLE_NONE;
	m_present.MultiSampleQuality			= 0;
	m_present.FullScreen_RefreshRateInHz	= D3DPRESENT_RATE_DEFAULT;
	m_present.PresentationInterval			= D3DPRESENT_INTERVAL_IMMEDIATE;
	m_present.Flags							= 0;

	//check gpu support for hardware vertex processing (faster)
	//else use cpu software vertex processing (slower)
	DWORD deviceFlag = 0;
	if(m_capability.DevCaps & D3DCREATE_HARDWARE_VERTEXPROCESSING)
		deviceFlag |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
	else
		deviceFlag |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;

	//if hardware processing is supported
	//check if a pure device can be created
	if((m_capability.DevCaps & D3DDEVCAPS_PUREDEVICE)
		&& (deviceFlag & D3DCREATE_HARDWARE_VERTEXPROCESSING))
		deviceFlag |= D3DCREATE_PUREDEVICE;

	//create direct3d device interface
	if(FAILED(m_object->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		m_window->m_hWnd,
		deviceFlag,
		&m_present,
		&m_device)))
		return GPX_INIT_DEVICE_ERROR;

	//create sprite interface
	if(FAILED(D3DXCreateSprite(m_device,&m_sprite)))
		return GPX_INIT_TEXTURE_ERROR;

	//create font interface
	if(FAILED(D3DXCreateFontA(m_device,
		16,0,FW_NORMAL,0,false,
		DEFAULT_CHARSET,
		OUT_DEVICE_PRECIS,
		PROOF_QUALITY,
		FIXED_PITCH | FF_DONTCARE,
		TEXT("Consolas"),
		&m_font)))
		return GPX_INIT_FONT_ERROR;

	//create line interface
	if(FAILED(D3DXCreateLine(m_device, &m_line)))
		return GPX_INIT_LINE_ERROR;

	return GPX_SUCCESS;
}

int Direct3DFramework::release()
{
	//perform safe releases
	for(int i = 0; i < m_textureList.size(); i++)
		m_textureList[i]->m_texture->Release();

	SAFE_RELEASE_LIST(m_textureList);

	SAFE_RELEASE_URN(m_object);
	SAFE_RELEASE_URN(m_device);
	SAFE_RELEASE_URN(m_sprite);
	SAFE_RELEASE_URN(m_font);
	SAFE_RELEASE_URN(m_line);

	return GPX_SUCCESS;
}